﻿#include "StatusReporterTask.h"
#include "Logger.h"
#include "TcpConnectHandler.h"
#include "syscfg.h"
#include "reportstatusreq.h"
#include "commonresp.h"
#include "appmonitortaskmanager.h"


StatusReporterTask gStatusReporterTask;

StatusReporter gStatusReporter;

StatusReporterTask::StatusReporterTask()
{
}

StatusReporterTask::~StatusReporterTask()
{

}

int StatusReporterTask::start()
{
    int lastState = ReportStatusState_disconnect;
    while (state == RunableStateStarted) {

        if(WATCHDOG_SERVER_IP.toStdString().length() == 0)
        {
            lastState = ReportStatusState_offlineWorkMode;
            emit gStatusReporter.reportStatusStateChanged(lastState);
            Sleep(1000);
            continue;
        }

        if(lastState == ReportStatusState_disconnect)
        {
            lastState = ReportStatusState_connecting;
            emit gStatusReporter.reportStatusStateChanged(lastState);
            Sleep(1000);
        }

        TcpConnectHandler * connect = TcpConnectHandler::connect(WATCHDOG_SERVER_IP.toStdString(), WATCHDOG_SERVER_PORT);
        if (connect)
        {

            ReportStatusReq * req = new ReportStatusReq(gAppMonitorTaskManager.getApps());

            if (f_success == connect->sendMsg(req))
            {
                LOG_INFO("try to send report status req to server ");
                CommonResp * res = (CommonResp *)connect->readMsg();
                if(res)
                {
                    if(res->code == CommonRespResult_success)
                    {
                        lastState = ReportStatusState_connected;
                        LOG_INFO("read report status res success");
                        emit gStatusReporter.reportStatusStateChanged(lastState);
                    }
                    else
                    {
                        lastState = ReportStatusState_disconnect;
                        LOG_ERROR("read report status res success but result code:%d failed, fail reason:%s", res->code, res->result.c_str());
                        emit gStatusReporter.reportStatusStateChanged(lastState);
                    }
                }
                else
                {
                    lastState = ReportStatusState_disconnect;
                    LOG_ERROR("try to read report status failed");
                    emit gStatusReporter.reportStatusStateChanged(lastState);
                }
                SafeDeleteObj(res);
            }
            else
            {
                lastState = ReportStatusState_disconnect;
                LOG_ERROR("try to send report status failed");
                emit gStatusReporter.reportStatusStateChanged(lastState);
            }
            SafeDeleteObj(connect);

            SafeDeleteObj(req);
        }
        else
        {
            lastState = ReportStatusState_disconnect;
            emit gStatusReporter.reportStatusStateChanged(lastState);
        }



        for (int i = 0; i < HEART_BEAT_INTERVAL_SECONDS; i++)
        {
            if(state == RunableStateStarted)
            {
                Sleep(1000);
            }
            else
            {
                break;
            }
        }
    }

    return f_success;
}

StatusReporter::StatusReporter()
{
}

StatusReporter::~StatusReporter()
{

}
